﻿using Lgc_VoteSys.Models.Database;
using Lgc_VoteSys.Models.Vote;
using Lgc_VoteSys.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Lgc_VoteSys.Controllers
{
    /// <summary>
    /// 投票列表模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class VoteController : ControllerBase
    {
        // 定义数据库上下文
        private readonly Lgc_VoteSysContext _db;
        private readonly IJWTService _jwtService;

        /// <summary>
        /// 构造方法 构造注入
        /// 把数据库上下文初始化
        /// </summary>
        /// <param name="db"></param>
        public VoteController(Lgc_VoteSysContext db, IJWTService jwtService)
        {
            _db = db;
            _jwtService = jwtService;
        }

        /// <summary>
        /// 获取投票项目列表接口
        /// </summary>
        /// <param name="item">类别、搜索关键字、分页信息</param>
        /// <returns></returns>
        [HttpPost]
        public List<Vote_GetVoteList_return> GetVoteList(Vote_GetVoteList item)
        {//返回logo 点赞数 公司名
            if(string.IsNullOrEmpty(item.type) && string.IsNullOrEmpty(item.key))
            {
                return null;
            }
            if (string.IsNullOrEmpty(item.count.ToString()) || string.IsNullOrEmpty(item.page.ToString()))
            {
                return null;
            }
            var data = _db.EnterpriseDetails.DistinctBy(x=>x.EdState).ToList();
            // 查出已通过审核的投票项目
            data = data.Where(x => x.EdType == 1).ToList();

            if (!string.IsNullOrEmpty(item.type)) // 如果类型不为空
            {
                data = data.Where(x => x.PvcName == item.type).ToList();
            }
            if(!string.IsNullOrEmpty(item.key)) // 如果关键字不为空
            {
                data = data.Where(x => x.EdName.ToLower().Contains(item.key.ToLower())).ToList();
            }

            // 计算最大页数
            int maxPage = 1;
            if (data.Count % item.count == 0)
            {
                maxPage = data.Count / item.count;
            }
            else
            {
                maxPage = data.Count / item.count + 1;
            }

            if (item.page <= 0 || item.count <= 0 || item.page > maxPage)
            {
                return null;
            }
            data = data.Skip((item.page - 1) * item.count).Take(item.count).ToList();
            List<Vote_GetVoteList_return> list = new List<Vote_GetVoteList_return>();
            foreach (var t in data)
            {
                var count = _db.Votes.Where(x => x.EdId == t.EdId).ToList();

                var _votecount = "0";
                if (count.Count <= 0)
                {
                    _votecount = "0";
                }
                else
                {
                    _votecount = count.FirstOrDefault().VCount.ToString();
                }

                list.Add(new Vote_GetVoteList_return
                {
                    logo=t.EdLogo,
                    votecount= _votecount,
                    edname=t.EdName
                });
            }
            return list;
        }


    }
}
